package org.example.servicefaculty.controller;

import org.example.model.manage.bean.Result;
import org.example.model.manage.bean.po.Counselor;
import org.example.model.manage.bean.po.Faculty;
import org.example.model.manage.bean.po.Major;
import org.example.model.manage.bean.po.Student;
import org.example.model.manage.bean.po.security.User;
import org.example.model.manage.bean.vo.*;
import org.example.model.manage.bean.po.security.MyUserDetails;
import org.example.servicefaculty.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@PreAuthorize("hasAnyAuthority('faculty')")
public class FacultyController {

    @Autowired
    private CounselorService counselorService;

    @Autowired
    private FacultyService facultyService;
    /**
     * 获取学院内的专业列表
     */
    @GetMapping("/majorList")
    public Result<List<Major>> getMajorList() {
        User currentUser = ((MyUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
        List<Major> list = facultyService.getMajorsByFacultyId(currentUser.getTypeId());
        return Result.success(list);
    }

    // ========== 统计功能 ==========

    /**
     * 获取学院统计信息
     */
    @GetMapping("/facultyCount")
    public Result<FacultyCount> getFacultyCount() {
        try {
            User currentUser = ((MyUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
            FacultyCount count = facultyService.getFacultyCount(currentUser.getTypeId());
            return Result.success(count);
        } catch (Exception e) {
            return Result.error(500, "获取统计信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取学院内各专业的统计信息
     */
    @GetMapping("/majorCount")
    public Result<List<MajorCount>> getMajorCount() {
        try {
            User currentUser = ((MyUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
            List<MajorCount> counts = facultyService.getMajorCountsByFacultyId(currentUser.getTypeId());
            return Result.success(counts);
        } catch (Exception e) {
            return Result.error(500, "获取专业统计信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取指定专业人员报道情况
     */
    @GetMapping("/getByMajor")
    public Result<List<Student>> getByMajor(@RequestParam Integer majorId) {
        try {
            User currentUser = ((MyUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
            List<Student> students = facultyService.getStudentsByMajorId(majorId, currentUser.getTypeId());
            return Result.success(students);
        } catch (Exception e) {
            return Result.error(500, "获取学生列表失败: " + e.getMessage());
        }
    }

    // ========== 兼容性接口（保留原有功能） ==========

    /**
     * 获取学院下所有辅导员及其负责的专业列表
     */
    @GetMapping("/getCounselor")
    public Result<List<CounselorWithMajorVO>> getCounselor() {
        User currentUser = ((MyUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
        List<CounselorWithMajorVO> list = counselorService.getCounselorWithMajorByFacultyId(currentUser.getTypeId());
        return Result.success(list);
    }
    /**
     * 更新辅导员负责的专业列表（新接口）
     */
    @PutMapping("/updateCounselorMajor")
    public Result<Boolean> updateCounselorMajors(@RequestBody UpdateCounselorMajorsRequest request) {
        Boolean success = counselorService.updateCounselorMajors(request.getCounselorId(), request.getMajorIds());
        return success ? Result.success(true) : Result.error(400, "修改失败");
    }
}